// pages/publish/publish.js

const App = getApp();
const CONFIG = require('../../config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    article: {
      title: '',
      content: '',
      img_url: ''
    },
    imgUrl: [],
    video: [],
    images_files_previw: [],
    content_list: [
      {
        img_url: '',
        text: ""
      }
    ]
  },

  changetext: function (type, content, index) {
    var _this = this;
    if (type == 'title') {
      _this.setData({
        'article.title': content
      })
    }

    if (type == 'text') {
      var _content_list = _this.data.content_list;
      _content_list[index].text = content;
      _this.setData({
        'content_list': _content_list
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindArticleTitle: function (e) {
    var _this = this;
    _this.setData({
      'article.title': e.detail.value
    })

  },
  bindArticleContent: function (e) {
    var _this = this;
    _this.setData({
      'article.content': e.detail.value
    })

  },
  publish: function () {

    var _this = this;
    var _article = _this.data.article;
    var _content_list = _this.data.content_list;
    _article.img_url = _article.img_url.split('.')[0]
    _article.content=(function(){
      var content='';
      _content_list.forEach(function(list,index){
        console.log(list);
        if (list.img_url){
          content = content + "<img src=\"../img/" + list.img_url + "\">";
        }
        if (list.text) {
          content = content + "<p>" + list.text+" </p>";
        }
      })
      return content
    })();

    if (!_article.title){
      return App.topTips.showTopTips.call(_this, '请输入页面标题');
    }

    if (!_article.img_url) {
      return App.topTips.showTopTips.call(_this, '请上传封面图片');
    }

    if (!_article.content) {
      return App.topTips.showTopTips.call(_this, '请输入文章内容或文章图片');
    }

    wx.showLoading({ title: 'loading~' });
    App.Api.post('/iNews/uploadNews', _article)
      .then((data) => {
        wx.hideLoading();
        console.log(_article);
        wx.navigateBack();
      })
      .catch((error) => {
        console.log('error', error);
        wx.hideLoading();
        App.topTips.showTopTips.call(_this, error.msg);
      })

  },
  eventChooseImage: function () {
    var _this = this;


    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // 返回选定照片的本地文件路径列表，
        //tempFilePath可以作为img标签的src属性显示图片
        wx.uploadFile({
          url: CONFIG.api + '/upload/uploadImg',
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data);
            _this.setData({
              'article.img_url': data.r.newName
            });
          }
        })

      }
    })
  },
  eventChooseContentImage: function (e) {
    console.log(e);
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var _content_list = _this.data.content_list;

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // 返回选定照片的本地文件路径列表，
        //tempFilePath可以作为img标签的src属性显示图片
        console.log(JSON.stringify(res));

        wx.uploadFile({
          url: CONFIG.api + '/upload/uploadImg',
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data);

            _content_list[index].img_url = data.r.newName;
            _this.setData({
              content_list: _content_list
            });
          }
        })

        console.log(_content_list);
        // _this.setImageFiles(_this.data.images.files.concat(res.tempFilePaths));
      }
    })

  },
  eventAddContent: function () {
    var _this = this;
    var _content_list = _this.data.content_list;
    _content_list.push({
      img_url: '',
      text: ""
    })
    _this.setData({
      'content_list': _content_list
    })
  },
  eventDelImage: function (e) {
    var _this = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var _content_list = _this.data.content_list;
    _content_list[index].img_url = '';
    wx.showModal({
      title: '提示',
      content: '确认删除这张照片?',
      success(res) {
        if (res.cancel) return;


        _this.setData({
          'content_list': _content_list
        });
      }
    })
  },
  eventDelContent: function (e) {
    var _this = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var _content_list = _this.data.content_list;

    wx.showModal({
      title: '提示',
      content: '确认删除该图文?',
      success(res) {
        if (res.cancel) return;

        _content_list.splice(index, 1);
        _this.setData({
          'content_list': _content_list
        });
      }
    })


  },

})